var map, myloc, t, markers={}, t1, tempmark
var zoomnumber = 12

//初始化地图
function init() {
    // map = new qq.maps.Map(document.getElementById("map_canvas"), {
        // center: new qq.maps.LatLng(mylocation.lat,mylocation.lng),      // 地图的中心地理坐标。
        // zoom:zoomnumber                                                 // 地图的中心地理坐标。
    // });
    map = new qq.maps.Map(document.getElementById("map_canvas"))
    var setcenter = true
    citylocation = new qq.maps.CityService({
        complete : function(result){
            if(setcenter){
            map.setCenter(result.detail.latLng);
            map.setZoom(zoomnumber)}
        }
    });
    citylocation.searchLocalCity();
    //此处开始绑定父框架上的按钮
    $(top.document.body).on('click', 'a.mapapplymode', function(){
        // console.info(this)
        self = $(this)
        self.removeClass('mapapplymode')
        self.addClass('mapapplycancel')
        self.find('.ui-btn-text').text('取消申请')
        //弹出菜单
        qq.maps.event.addListener(map, 'rightclick', function(e){
            // console.info('rightclick')
            // clearTimeout(t)
            menuup(e);        
        })
        // qq.maps.event.addDomListener($('#map_canvas')[0], 'mouseup', function(e){
            // clearTimeout(t)
        // })        
        // qq.maps.event.addListener(map, 'mousemove', function(e){
            // clearTimeout(t)
        // })
        // qq.maps.event.addListener(map, 'bounds_changed', function(e){
            // clearTimeout(t)
        // })
        // qq.maps.event.addListener(map, 'zoom_changed', function(e){
            // clearTimeout(t)
        // })
        // qq.maps.event.addListener(map, 'center_changed', function(e){
            // clearTimeout(t)
        // })        
        // qq.maps.event.addListener(map, 'dragstart', function(e){
            // clearTimeout(t)
        // })
        // qq.maps.event.addListener(map, 'zoom_changed', function(e){
            // clearTimeout(t)
        // }) 
        // qq.maps.event.addListener(map, 'idle', function(e){
            // clearTimeout(t)
        // })         
        
        qq.maps.event.addListener(map, 'mousedown', function(e){
            // console.info('mousedown')
            myloc = e.latLng
            $('.myMenu').slideUp()        
            // t = setTimeout(function(){menuup(e)}, 1000)
        })
        //通过直接绑定#map_canvas形成长按效果
        $(document).on('touchstart touchmove touchend', '#map_canvas', function(e){
            switch(e.type){
                case 'touchstart': 
                    $('.myMenu').slideUp();
                    if(e.originalEvent.touches.length==1){t1 = setTimeout(function(){menuup1(e);}, 1000)}
                    break;
                case 'touchmove':
                    clearTimeout(t1);
                    break;
                case 'touchend':
                    clearTimeout(t1);
                    break;
            }
        })         
        qq.maps.event.addListener(map, 'click', function(e){
            clearTimeout(t)

        })
        
    })
    //绑定取消申请按钮
    $(top.document.body).on('click', 'a.mapapplycancel', function(){
        self = $(this)
        self.addClass('mapapplymode')
        self.removeClass('mapapplycancel')
        self.find('.ui-btn-text').text('申请模式')
        // qq.maps.event.clearListeners(map, 'idle')        
        // qq.maps.event.clearListeners(map, 'dragstart')
        // qq.maps.event.clearListeners(map, 'dblclick')
        // qq.maps.event.clearListeners(map, 'click')
        // qq.maps.event.clearListeners(map, 'mousedown')
        qq.maps.event.clearListeners(map, 'rightclick')
        $(document).off('touchstart touchmove touchend', '#map_canvas')
        tempmark.setMap(null);
    })

    info = new qq.maps.InfoWindow({
        map: map
    });
    $.getJSON('/agm/sellmap?action=js').done(function(data){
        // console.info(data)
        ret = data
        $.each(ret, function(i, e){
            var center = new qq.maps.LatLng(e.lat, e.lng)
            var marker = new qq.maps.Marker({
                position: center,
                map: map
            })
            markers[e.objectId] = marker
            qq.maps.event.addListener(marker, 'click', function(){
                info.open()
                info.setPosition(this)
                info.setContent(getinfo(e))
            })
            label = new qq.maps.Label({
                position: center,
                map: map,
                content: e.title
            })
        })
        //此处开始绑定父框架上的按钮
        $(top.document.body).find('select#shopselect').each(function(){
            if(this.value!='local'){
                qq.maps.event.trigger(markers[this.value], 'click')
                map.setZoom(zoomnumber)
                setcenter = false;
            }
        })
        $(top.document.body).on('change', 'select#shopselect', function(){
            if(this.value=="local"){
                map.setCenter(new qq.maps.LatLng(mylocation.lat,mylocation.lng))
                map.setZoom(zoomnumber)
            }else{            
                qq.maps.event.trigger(markers[this.value], 'click')
            }
        })
    })
}
//用于info显示模版
getinfo = function(e){
    img = e.hasOwnProperty('imgurl')?'<div class="shopimage"><img src="'+e.imgurl+'"></div>':''
    div = $('<div class="shopinfo">'+img+'<div class="title">店名:'+e.title
        +'</div><div class="addr">地址:'+ e.addr
        +'</div><div class="phone">电话:'+ e.phone
        +'</div></div>')
    return div[0]
}

//callback 城市位置
callback = function(){
    cb_url = 'http://api.go2map.com/engine/api/ipcity/json?cb=setmap'
    $('<script src="'+cb_url+'"></script>').appendTo('body')
}
//设置ip的callback
setip = function(ret){
    // console.info(ret)
    mylocation = ret.result.location
    myad_info = ret.result.ad_info
}

//弹出右键菜单并设置myloc的值
menuup = function(e){
    // console.info(e.pixel)
    tempmarkit()
    $('.myMenu').css({'top': e.pixel.y, 'left': e.pixel.x}).slideDown()
    myloc = e.latLng    
}
menuup1 = function(e){
    tempmarkit()
    var touch = e.originalEvent.touches[0]
    $('.myMenu').css({'top': touch.pageY, 'left': touch.pageX}).slideDown()
}
//临时mark标志与动画
tempmarkit = function(){
    if(!tempmark){
    tempmark = new qq.maps.Marker({
        position: new qq.maps.LatLng(myloc.lat, myloc.lng),
        map: map,
        draggable:true,
        animation:qq.maps.MarkerAnimation.DROP
            
    })
    qq.maps.event.addListener(tempmark, 'click', function(e){
        $('.myMenu').css({'top': e.pixel.y, 'left': e.pixel.x}).slideDown()
    })
    }else{
        tempmark.setMap(map);
        tempmark.setPosition(new qq.maps.LatLng(myloc.lat, myloc.lng))
    }
}

//绑定申请标志的动作
$('.applymark').bind('click', function(){
    // console.info(myloc)
    window.top.location.href ='/agm/sellmap?action=setting&lat='+tempmark.getPosition().lat+'&lng='+tempmark.getPosition().lng
})

